#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <string.h>
#define nightMoney 30
#define hourMoney 10
typedef struct parkingSpace
{
    int dif23;          //停车时距离当天23点的小时差，不满h按照1h算;
    int Num;            //车位号;
    int state;          //作为flag,车位可用则state=1;
    int night;          //作为flag,车辆过夜变为night=1;
    char carMaster[16]; //车主
    char carNum[16];    //车牌
    time_t time;
    char locTime[20]; //停车时间；
} parkingSpace;
parkingSpace a[20];
int carPlaceNum(parkingSpace a[]);
void mainMenu(void);
void findMenu(void);
void allFind(void);
void spaceFind(void);
void addMenu(void);
void carNumFind(void);
void feeMenu(void);
void carLeave(void);
int main(void)
{
    for (int i = 1; i <= 20; i++)
    {
        a[i - 1].Num = i;
        a[i - 1].state = 1;
        a[i - 1].night = 0;
        a[i - 1].dif23 = 0;
    }
    while (1)
    {
        mainMenu();
    };
    return 0;
}
void mainMenu(void)
{
    system("cls");
    char n;
    printf("********************* 停车场管理系统 *********************\n");
    printf("            1,查询                   2,增加\n");
    printf("            3,计费                   4,车辆离开\n");
    printf("            0,退出程序\n");
    printf("**********************************************************\n");
label1:
    scanf("%c", &n);
    getchar();
    switch (n)
    {
    case '1':
        findMenu();
        break;
    case '2':
        addMenu();
        break;
    case '3':
        feeMenu();
        break;
    case '4':
        carLeave();
        break;
    case '0':
        exit(0);
    default:
        printf("error!\n");
        goto label1;
    };
}
void findMenu(void)
{
    system("cls");
    printf("**************************查询****************************\n");
    printf("       1、查询所有     2、按车牌号查询\n");
    printf("       3、按车位查询   0、返回上级\n");
    printf("**********************************************************\n");
    printf("现有空余车位%d个\n", carPlaceNum(a));
    char n;
label2:
    scanf("%c", &n);
    getchar();
    switch (n)
    {
    case '1':
        allFind();
        break;
    case '2':
        carNumFind();
        break;
    case '3':
        spaceFind();
        break;
    case '0':
        return;
        break;
    default:
        printf("error!!");
        goto label2;
    }
}
void allFind(void)
{
    system("cls");
    for (int i = 0; i < 20; i++)
    {
        if (a[i].state == 0)
            printf("车位:%d 车主:%s 车牌:%s 停车时间:%s 是否过夜%d:\n", a[i].Num, a[i].carMaster, a[i].carNum, a[i].locTime, a[i].night);
        else if (a[i].state == 1)
        {
            printf("车位:%d  车主:null  车牌:null  停车时间:null\n", a[i].Num);
        }
    }
    printf("按ctrl回到主菜单");
    getchar();
    return;
}
void feeMenu(void)
{
    system("cls");
    printf("************************请输入结算车车牌************************\n");
    char carNum[16];
    time_t nowTime;
    time(&nowTime);
    struct tm *info = localtime(&nowTime);
    int fee;
    int n = 0;
    scanf("%s", carNum);
    for (int i = 0; i < 20; i++)
    {
        if (strcmp(a[i].carNum, carNum) == 0)
        {
            printf("车位：%d  车主：%s  车牌：%s  停车时间:%s\n", a[i].Num, a[i].carMaster, a[i].carNum, a[i].locTime);
            n++;
            if (a[i].night == 0)
            {
                if ((nowTime - a[i].time) % 3600 == 0)
                {
                    fee = ((nowTime - a[i].time) / 3600) * hourMoney;
                }
                else
                    fee = (((nowTime - a[i].time) / 3600) + 1) * hourMoney; //不满1h按1h算
            }                                                               //如果不包夜，停车费用则为(停车时到查询时小时数)*hourMoney
            else if (a[i].night == 1)
            {
                if (nowTime - a[i].time > 9 * 3600)
                {
                

                  if ((nowTime - a[i].time) % 3600 == 0)
                {
                   fee = ((nowTime - a[i].time) / 3600 - 9) * hourMoney + nightMoney;
                }
                else   
                    fee = ((nowTime - a[i].time) / 3600 - 9+1) * hourMoney + nightMoney;
                }
                else if (nowTime - a[i].time <= 9 * 3600)
                {
                    if (info->tm_hour < 23)
                    {

                        if ((nowTime - a[i].time) % 3600 == 0)
                        {
                            fee = ((nowTime - a[i].time) / 3600) * hourMoney;
                        }
                        else
                            fee = (((nowTime - a[i].time) / 3600) + 1) * hourMoney;
                    } //如果查询时还没到当天23点，则费用停车时到查询时小时数*hourMoney
                    else
                    {
                        fee = a[i].dif23 * hourMoney + nightMoney;
                    }
                }
            } //如果包夜且停车时间超过9h，停车费用为nightMoney+(停车时到查询时小时数-9）
              //如果包夜但停车时间不足9和，费用为nightMoney+dif23;
        }
    }
    if (n == 0)
        printf("查无此车\n");
    else
    {
        printf("该车截至目前停车费用为%d\n", fee);
        printf("按ctrl回到主菜单");
    }
    getchar();
    getchar();
    return;
}
void spaceFind(void)
{
    system("cls");
    printf("*************************车位查询*************************\n");
    int num;
    printf("请输入要查询的车位号(1~20)\n");
    scanf("%d", &num);
    int i = num - 1;
    if (num > 20 || num < 1)
    {
        printf("error\n");
    }
    else if (a[num - 1].state == 0)
    {
        printf("车位:%d 车主:%s 车牌:%s 停车时间:%s 是否过夜:%d\n", a[i].Num, a[i].carMaster, a[i].carNum, a[i].locTime, a[i].night);
    }
    else if (a[num - 1].state == 1)
    {
        printf("车位%d号可用\n", num);
    }
    printf("按ctrl回到主菜单");
    getchar();
    getchar();
    return;
}
void carNumFind(void)
{
    system("cls");
    char car[20];
    int n = 0;
    printf("请输入车牌号\n");
    scanf("%s", car);
    for (int i = 0; i < 20; i++)
    {
        if (strcmp(car, a[i].carNum) == 0)
        {
            n = 1;
            printf("车位：%d  车主：%s  车牌：%s  停车时间:%s\n", a[i].Num, a[i].carMaster, a[i].carNum, a[i].locTime);
        }
    }
    if (n == 0)
    {
        printf("查无此车\n");
    }
    printf("按ctrl回到主菜单");
    getchar();
    getchar();
    return;
}
void addMenu(void)
{
    system("cls");
    int n = 0;
    printf("*************************请选择车位************************\n");
    for (int i = 0; i < 20; i++)
    {
        if (a[i].state)
        {
            printf("%d\t", a[i].Num);
            n++;
            if (n % 4 == 0)
            {
                printf("\n");
            }
        }
    }
    printf("\n");
    printf("*************************请选择车位************************\n");
    int num;
    if (carPlaceNum(a) == 0)
    {
        printf("已无车位");
        getchar();
        getchar();
        return;
    }
    scanf("%d", &num);
    if (num > 20 || num < 1)
    {
        printf("error\n");
        printf("按下ctrl回到主菜单");
        getchar();
        getchar();
        return;
    }
    if (a[num - 1].state == 0)
    {
        printf("此车位已被占用\n");
        printf("按下ctrl回到主菜单");
        getchar();
        getchar();
        return;
    }

    printf("请输入车主姓名\n");
    scanf("%s", a[num - 1].carMaster);
    printf("请输入车牌号\n");
    scanf("%s", a[num - 1].carNum);
    printf("当晚车辆是否过夜 y/n\n");
    char bol;
    getchar();
    scanf("%c", &bol);
    char tim[16];
    struct tm *info;
    time(&a[num - 1].time);
    info = localtime(&a[num - 1].time);
    strftime(tim, 80, "%Y-%m-%d %H:%M:%S", info);
    strcpy(a[num - 1].locTime, tim);
    if (bol == 'y' || bol == 'Y')
    {
        a[num - 1].night = 1;
    }
    a[num - 1].state = 0;
    if (23 - info->tm_hour <= 0)
    {
        a[num - 1].dif23 = 0;
    }
    else
    {
        a[num - 1].dif23 = 23 - info->tm_hour;
    }
    printf("停车时间%s\n", a[num - 1].locTime);
    printf("停车成功\n");
    printf("按ctrl回到主菜单");
    getchar();
    getchar();
    return;
    // printf("%s  %s  %ld",a[num-1].carMaster,a[num-1].carNum,a[num-1].time);
}
int carPlaceNum(parkingSpace a[])
{
    int num = 0;
    for (int i = 0; i < 20; i++)
    {
        if (a[i].state == 1)
        {
            num++;
        }
    }
    return num;
}
void carLeave(void)
{
    system("cls");
    printf("************************请输入离去车车牌************************\n");
    char name[16];
    int n = 0;
    scanf("%s", name);
    for (int i = 0; i < 20; i++)
    {
        if (strcmp(a[i].carNum, name) == 0)
        {
            n++;
            a[i].state = 1;
        }
    }
    if (n == 0)
        printf("查无此车\n");
    else
        printf("车辆离去成功\n");
    printf("按ctrl回到主菜单");
    getchar();
    getchar();
    return;
}